const Service = require('egg').Service;

class GradeService extends Service {

  // user表

  async findUsers() {
    const data = await this.app.mysql.select('users');
    return { code: 0, data }
  }


  // 操作grade表
  async addGrade() {
    const { app } = this;
    console.log('后端添加数据：', this.ctx.request.body)
    const { stuid, username, theory, skill, } = this.ctx.request.body;
    // const data = await app.mysql.get('users', { stuid })
    // if (data) {
    const data = await app.mysql.insert('grade', {
      stuid, theory, skill, username
    })
    return { code: 0, data }
    // } else {
    //   return { code: -1, msg: 'fail' }
    // }
  }

  async updateGrade() {
    const { app } = this;
    const { id, theory, skill } = this.ctx.request.body;
    const data = await app.mysql.query(`
     update grade set theory=?, skill=? where id=?
    `, [theory, skill, id])
    return { code: 0, data }
  }

  async pageGrade() {
    const { page, size } = this.ctx.query;
    const total = await this.app.mysql.select('grade');
    const results = await this.app.mysql.select('grade', { // 搜索 post 表
      limit: size * 1, // 返回数据量
      offset: page * size, // 数据偏移量
    });
    return { code: 0, data: results, total: total.length }
  }
}

module.exports = GradeService;